package com.hnepri.fang.ftx.fetch.residential;

import com.hnepri.fang.ftx.model.Residential;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 小区简介分页列表页解析
 */
public class ResidentialPageParser {
    /**
     * 解析当前地市有多少页数据
     * @param page
     * @return
     */
    public static int getPageSize(String page) {
        Document doc = Jsoup.parseBodyFragment(page);
        String pageSize = doc.select("#houselist_B14_01 span.txt").text();
        pageSize = pageSize.replace("共", "").replace("页", "");
        System.out.println(pageSize);
        return Integer.parseInt(pageSize);
    }

    /**
     * 解析一页面数据
     * @param page
     * @return
     */
    public static List<Residential> parse(String page) {
        Document doc = Jsoup.parseBodyFragment(page);
        Elements elements = doc.select("div.houseList>div.list");
        List<Residential> dataList = getResidentialList(elements);
        return dataList;
    }

    /**
     * 解析小区dom列表为对象
     * @param elements
     * @return
     */
    private static List<Residential> getResidentialList(Elements elements) {
        List<Residential> dataList = new ArrayList<>();
        for (int i = 0; i < elements.size(); i++) {
            dataList.add(getResidential(elements.get(i)));
        }
        return dataList;
    }

    /**
     * 把dom数据解析为java对象
     * @param element
     * @return
     */
    private static Residential getResidential(Element element) {
        Residential residentialQuarters = new Residential();
        try {
            Element urlEle = element.select("dd>p:eq(0)>a").get(0);
            String url = urlEle.attr("href");
            String projname = urlEle.text();
            String type = element.select("dd>p:eq(0)>span.plotFangType").text();
            String district = element.select("dd>p:eq(1)>a:eq(0)").text();
            String comarea = element.select("dd>p:eq(1)>a:eq(1)").text();
            List<Node> pDom2 = element.select("dd>p:eq(1)").get(0).childNodes();
            String address = pDom2.get(pDom2.size() - 1).toString();
            String price = element.select("div.listRiconwrap>p.priceAverage span:eq(0)").text();
            residentialQuarters.setDomain(url);
            residentialQuarters.setProjname(projname);
            residentialQuarters.setType(type);
            residentialQuarters.setDistrict(district);
            residentialQuarters.setComarea(comarea);
            residentialQuarters.setAddress(address);
            residentialQuarters.setPrice(price);
        } catch (Exception e) {
            System.out.println(element.html());
            e.printStackTrace();
            throw e;
        }
        return residentialQuarters;
    }

    /**
     * 把一页数据写入文件
     * @param path
     * @param ls
     */
    public static void writeResidential(String path, List<Residential> ls) {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path), "utf-8"));
            StringBuilder sb = new StringBuilder();
            for(Residential obj: ls) {
                sb.append(obj.getProjname() + "|");
                sb.append(obj.getDistrict() + "|");
                sb.append(obj.getType() + "|");
                sb.append(obj.getComarea() + "|");
                sb.append(obj.getPrice() + "|");
                sb.append(obj.getAddress() + "|");
                sb.append(obj.getDomain() + "\r\n");
            }

            pw.println(sb.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) pw.close();
        }
    }
}
